﻿using System;
using System.Linq;
using NHibernate.Linq;
using SellAndBuy.Application.Query.DTOs;
using SellAndBuy.Application.Query.Finders;
using SellAndBuy.Infrastructure.Data.Core;
using SellAndBuy.Infrastructure.Data.Core.Finders;

namespace SellAndBuy.Infrastructure.Data.Query.Finders
{
    /// <summary>
    /// Customer Finder
    /// </summary>
    public class CustomerFinder : Finder<CustomerDTO>, ICustomerFinder
    {
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="unitOfWork"></param>
        public CustomerFinder(INhibernateUnitOfWork unitOfWork)
            : base(unitOfWork)
        {
        }

        /// <summary>
        /// Find customer by email
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">Argument is null.</exception>
        public CustomerDTO FindByEmail(string email)
        {
            if(string.IsNullOrWhiteSpace(email))
                throw new ArgumentNullException("email");

            return _unitOfWork.Session.Query<CustomerDTO>().Where(customer => customer.Email == email).SingleOrDefault();
        }

    }
}